import random

# 棋子类
class ChessPiece:
    def __init__(self, name, cost, attack, health):
        self.name = name
        self.cost = cost
        self.attack = attack
        self.health = health

    def __str__(self):
        return f"{self.name} (攻击: {self.attack}, 生命值: {self.health})"

# 玩家类
class Player:
    def __init__(self, name):
        self.name = name
        self.gold = 10
        self.experience = 0
        self.level = 1
        self.board = []
        self.shop = []
        self.refresh_shop()

    def refresh_shop(self):
        # 简单随机生成棋子到商店，实际可根据概率表优化
        all_pieces = [
            ChessPiece("战士", 1, 10, 20),
            ChessPiece("法师", 2, 15, 15),
            ChessPiece("刺客", 3, 20, 10)
        ]
        self.shop = random.sample(all_pieces, 3)

    def buy_piece(self, index):
        if index < len(self.shop) and self.gold >= self.shop[index].cost:
            piece = self.shop[index]
            self.gold -= piece.cost
            self.board.append(piece)
            self.refresh_shop()
            print(f"{self.name} 购买了 {piece.name}，剩余金币: {self.gold}")
        else:
            print("购买失败，金币不足或索引无效。")

    def sell_piece(self, index):
        if index < len(self.board):
            piece = self.board.pop(index)
            self.gold += piece.cost
            print(f"{self.name} 出售了 {piece.name}，获得 {piece.cost} 金币，剩余金币: {self.gold}")
        else:
            print("出售失败，索引无效。")

    def show_shop(self):
        print(f"{self.name} 的商店:")
        for i, piece in enumerate(self.shop):
            print(f"{i}. {piece}，价格: {piece.cost} 金币")

    def show_board(self):
        print(f"{self.name} 的棋盘:")
        for i, piece in enumerate(self.board):
            print(f"{i}. {piece}")

# 战斗函数
def battle(player1, player2):
    player1_board = player1.board.copy()
    player2_board = player2.board.copy()

    while player1_board and player2_board:
        p1_piece = player1_board[0]
        p2_piece = player2_board[0]

        p2_piece.health -= p1_piece.attack
        if p2_piece.health <= 0:
            player2_board.pop(0)
        else:
            p1_piece.health -= p2_piece.attack
            if p1_piece.health <= 0:
                player1_board.pop(0)

    if player1_board:
        print(f"{player1.name} 获胜！")
        player1.gold += 3
    elif player2_board:
        print(f"{player2.name} 获胜！")
        player2.gold += 3
    else:
        print("平局！双方都没有剩余棋子。")

# 主游戏循环
def main():
    player1 = Player("玩家 1")
    player2 = Player("玩家 2")
    round_num = 1

    while True:
        print(f"\n第 {round_num} 回合开始！")
        print("玩家 1 行动:")
        while True:
            player1.show_shop()
            print("操作选项: 1. 购买棋子 2. 出售棋子 3. 结束回合")
            choice = input("请输入选项: ")
            if choice == "1":
                index = int(input("请输入要购买的棋子索引: "))
                player1.buy_piece(index)
            elif choice == "2":
                index = int(input("请输入要出售的棋子索引: "))
                player1.sell_piece(index)
            elif choice == "3":
                break
            else:
                print("无效选项，请重新输入。")

        print("玩家 2 行动:")
        while True:
            player2.show_shop()
            print("操作选项: 1. 购买棋子 2. 出售棋子 3. 结束回合")
            choice = input("请输入选项: ")
            if choice == "1":
                index = int(input("请输入要购买的棋子索引: "))
                player2.buy_piece(index)
            elif choice == "2":
                index = int(input("请输入要出售的棋子索引: "))
                player2.sell_piece(index)
            elif choice == "3":
                break
            else:
                print("无效选项，请重新输入。")

        print("战斗开始！")
        battle(player1, player2)
        round_num += 1
        # 简单示例，可添加更复杂的回合结束处理逻辑，如经验获取、升级等
        player1.refresh_shop()
        player2.refresh_shop()

        # 可添加游戏结束条件判断，如某玩家金币为 0 或棋子全部失去等
        continue_game = input("是否继续游戏？(y/n): ")
        if continue_game.lower() != 'y':
            break

if __name__ == "__main__":
    main()